﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MethodWorx.CMS.Configuration;

namespace MethodWorx.CMS.Plugins.GlobalProperties
{
    public class GlobalPropertiesModel
    {
        private IEnumerable<IPropertyConfiguration> properties;

        public GlobalPropertiesModel(IEnumerable<IPropertyConfiguration> properties)
            :this(properties, null)
        {
            
        }

        public GlobalPropertiesModel(IEnumerable<IPropertyConfiguration> properties, PropertyDetails<object> type)
        {
            if(properties == null)
                throw new ArgumentException("properties");

            this.properties = properties;
            if (type == null)
            {
                this.Selected = properties.First();

                this.PropertyDetails = new PropertyDetails<object>
                                           {
                                               Custom = Activator.CreateInstance(this.Selected.GetDetailsType()),
                                               Group = "",
                                               Index = 0,
                                               Mandatory = false,
                                               Name = "",
                                               Type = this.Selected.Name
                                           };
            }
            else
            {
                this.Selected = properties.First(pc => pc.Name == type.Type);
                this.PropertyDetails = type;
            }
        }

        public SelectList GetPropertyTypeSelectList()
        {
            var items = properties.Select(pc => pc.Name);

            return new SelectList(items, this.Selected.Name);
        }

        public IPropertyConfiguration Selected { get; set; }

        public PropertyDetails<object> PropertyDetails { get; set; }
    }
}
